import csv
import os
from datetime import datetime
import time
#https://blog.csdn.net/qq7835144/article/details/88919624
#https://www.cnpython.com/qa/921828
def rfinfo(rootPath,filename):
    print('读取')
    with open(rootPath+filename, 'r') as csv_file:
        readerList = csv.reader(csv_file)
        tagfile=rootPath+'/new/'+filename
        if not os.path.exists(tagfile):  #文件不存在
            with open(tagfile, 'w', encoding='utf8', newline='') as newfile:
                writer = csv.writer(newfile)
        with open(tagfile, 'a+', encoding='utf8', newline='') as newfile:
            writer = csv.writer(newfile)
            #writer.writerow([1, 10, 20])
            for i, row in enumerate(readerList):
                if i>0: 
                    print(row)
                    t1=row[0].rstrip()
                    print('t1=',t1,'@')
                    time_format = "%Y-%m-%d %H:%M:%S"
                    time_obj = datetime.strptime(t1, time_format)
                    dd=str(time_obj.year)+'.'+str(time_obj.month)+'.'+str(time_obj.day)
                    hh=str(time_obj.hour)+':'+str(time_obj.minute)

                    hang=[dd,hh,row[1],row[2],row[3],row[4],row[5],row[6],row[7]]
                    #print('处理后=',hang)
                    writer.writerow(hang)
            newfile.close    
            csv_file.close    



if __name__=='__main__':
    rootdir='C:/Users/zhanghao/Desktop/期货指数1分钟-7月26/期货指数1分钟/'
    list=os.listdir(rootdir)
    for i in range(0,len(list)):
        path=os.path.join(rootdir,list[i])
        file_name = os.path.basename(path)
        if os.path.isfile(path=path):
            #print(path)
            if path.endswith(".csv"):
                print('确认路径',path,file_name)
                rfinfo(rootPath=rootdir,filename=file_name)